package com.hsy.crm.web.utils;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.po.account.DivResult;

import java.math.BigDecimal;

/**
 * 精确的加减乘除等处理
 */
public class CalcUtils {

    private CalcUtils(){}

    public static Long add(Long num1,Long num2){
        return new Double(add(num1.doubleValue(),num2.doubleValue())).longValue();
    }

    public static Long mul(Long num1,Long num2){
        return new Double(mul(num1.doubleValue(),num2.doubleValue())).longValue();
    }

    public static Long div(Long num1,Long num2){
        return new Double(div(num1.doubleValue(),num2.doubleValue())).longValue();
    }

    public static Long div100(Long num){
        return new Double(div(num,App.DECIMAL.DIV)).longValue();
    }

    public static Double div_price(Double num){
        return new Double(div(num,App.DECIMAL.DIV));
    }

    public static Double mul_price(Double num){
        return new Double(mul(num,App.DECIMAL.DIV));
    }

    /**
     * num1 + num2
     * @param num1
     * @param num2
     * @return
     */
    public static double add(double num1,double num2){
        BigDecimal data1 =  BigDecimal.valueOf(num1);
        BigDecimal data2 =  BigDecimal.valueOf(num2);
        return data1.add(data2).doubleValue();
    }

    /**
     * num1 - num2
     * @param num1
     * @param num2
     * @return
     */
    public static double sub(double num1,double num2){
        BigDecimal data1 =  BigDecimal.valueOf(num1);
        BigDecimal data2 =  BigDecimal.valueOf(num2);
        return data1.subtract(data2).doubleValue();
    }

    /**
     * num1 * num2
     * @param num1
     * @param num2
     * @return
     */
    public static double mul(double num1,double num2){
        BigDecimal data1 =  BigDecimal.valueOf(num1);
        BigDecimal data2 =  BigDecimal.valueOf(num2);
        return data1.multiply(data2).doubleValue();
    }

    /**
     * num1 ÷ num2 默认2位小数
     * @param num1
     * @param num2
     * @return
     */
    public static double div(double num1,double num2){
        return div(num1,num2,App.DECIMAL.LENGTH);
    }

    /**
     * num1 ÷ num2 scale位小数
     * @param num1
     * @param num2
     * @return
     */
    public static double div(double num1,double num2,int scale){
        if(scale<0){
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal data1 =  BigDecimal.valueOf(num1);
        BigDecimal data2 =  BigDecimal.valueOf(num2);
        return data1.divide(data2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 总金额除以总数  除不尽  那么就有两个金额  总数每个金额相加之和等于总金额
     * @param number
     * @param totalPrice
     * @return
     */
    public static DivResult result(long number, Long totalPrice){

        totalPrice = new Double(mul(totalPrice,App.DECIMAL.DIV)).longValue();

        if( totalPrice == 0 ){
            return new DivResult(0l);
        }

        long result = totalPrice / number ;
        long remainder = totalPrice % number;

        if( remainder == 0 ){
            return new DivResult(result,0L);
        }else{
            double strong = mul(result,(number-App.DECIMAL.DIV));
            strong = sub(totalPrice,strong);
            strong = div(strong,App.DECIMAL.DIV);

            return new DivResult(result,new Double(strong).longValue());
        }
    }

    /**
     * 取double值的小数值  抛弃整数部分
     * @param number
     * @return
     */
    public static double getDecimal(Double number){
        return CalcUtils.sub(number,int2double(number.intValue()));
    }

    public static double int2double(int number){
        return Double.parseDouble(String.valueOf(number));
    }

}
